/*
 * Copyright 2010 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.mnit.basis.swt.element.steuer.rollbar.gruppe.bild;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

import de.mnit.basis.daten.konstant.position.POSITION;
import de.mnit.basis.fehler.Fehler;
import de.mnit.basis.fehler.system.F_Sys_Datei;
import de.mnit.basis.grafik.BildRechner;
import de.mnit.basis.swt.dialog.SWT_Fehler;
import de.mnit.basis.swt.grafik.Bild_SWT;
import de.mnit.basis.swt.grafik.S_Bild_SWT;

/**
 * @author Michael Nitsche
 *
 * Siehe auch E_Bild TODO mergen
 */
public class SWT_Bild extends A_SWT_Bild<SWT_Bild, Canvas> {

	private PaintListener paintlistener=null;
	private final POSITION position;
	private Image aktuell=null;
	private Object puffer_bild=null;


	public SWT_Bild() {
		this(POSITION.MITTE);
	}

	public SWT_Bild(POSITION pos) {
		this.position=pos;
	}

	public SWT_Bild(POSITION pos, Object bild) {
		this.puffer_bild=bild; this.position=pos;
	}

	// FUNKTION

	public SWT_Bild sBild(Object datei) {
		Image bildt=null;
		try {
			bildt = Bild_SWT.neu_Direkt(datei).gImage();
		}
		catch(F_Sys_Datei e1) {
			SWT_Fehler.zeig(e1,true);
		}
		final Image bild=bildt;

		if(bild!=null) {
			if(bild.isDisposed()) SWT_Fehler.neu("Bild wurde bereits freigegeben!", true);
			if(paintlistener!=null) swt().removePaintListener(paintlistener);
			swt().addPaintListener(paintlistener=new PaintListener() {
				public void paintControl (PaintEvent e) {
					Rectangle rahmenbild=bild.getBounds();
					Rectangle rahmencanv=swt().getBounds();
					int[] p=BildRechner.ausrichtung(rahmencanv.width, rahmencanv.height, rahmenbild.width, rahmenbild.height, position);
					e.gc.drawImage(bild, p[0], p[1]);
				}
			});
			swt().redraw();

			if(aktuell!=null) aktuell.dispose();
			aktuell=bild;
		}
		return this;
	}

	// LESEN

	public S_Bild_SWT gBild() {
		if(this.aktuell==null)
			return null;
		try {
			return Bild_SWT.neu(this.aktuell);
		}
		catch(F_Sys_Datei e) {
			throw Fehler.sonstige.da_Unmoeglich();
		}
	}

	// ERZEUGEN

	protected int style6() {
		return SWT.NONE;
	}

	protected Canvas roh(Composite basis, int style) {
		return new Canvas(basis,style);
	}

	protected void init6() {
		this.eFreigabe(new Listener(){
			public void handleEvent(Event event) {
				if(aktuell!=null && !aktuell.isDisposed())
					aktuell.dispose();
			}
		});
		if(this.puffer_bild!=null)
			this.sBild(this.puffer_bild);
	}

}
